<template>
  <a-modal
    :destroy-on-close="true"
    :mask-closable="false"
    :visible="visible"
    :title="title"
    :confirm-loading="loading"
    @cancel="handleClose"
    @ok="handleSubmit"
  >
    <a-form ref="formRef" :model="formData" :rules="formRules" :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }">
      <a-form-item label="上级部门" name="parentId">
        <a-tree-select
          v-model:value="formData.parentId"
          style="width: 100%"
          :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
          :tree-data="selectDataSource"
          placeholder="请选择上级部门"
          tree-default-expand-all
          allow-clear
        >
        </a-tree-select>
      </a-form-item>
      <a-form-item label="部门名称" name="deptName">
        <a-input v-model:value="formData.deptName" placeholder="请输入部门名称" />
      </a-form-item>
      <a-form-item label="排序" name="sort">
        <a-input-number v-model:value="formData.sort" :min="0" :max="99" />
      </a-form-item>
      <a-form-item label="状态" name="status">
        <a-radio-group v-model:value="formData.status" button-style="solid">
          <a-radio-button v-for="item in AvailableStatusEnum.$options()" :key="item.value" :value="item.value">{{
            item.label
          }}</a-radio-button>
        </a-radio-group>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script name="SystemDeptEdit" lang="ts" setup>
import { getDept, saveDept, updateDept } from "@/api/system/dept";
import { enum2Value } from "@/utils/conver";
import { AvailableStatusEnum } from "@/enums/common";

const title = ref("添加部门");
const loading = ref(false);
const visible = ref(false);
const selectDataSource = inject("selectDataSource");
const emit = defineEmits(["reload"]);

const formRef = ref();
const formData = reactive<any>({
  deptId: undefined,
  parentId: 0
});
const formRules = reactive({
  deptName: [{ required: true, trigger: "blur", message: "请输入部门名称" }]
});

const handleGetDept = (id: number) => {
  loading.value = true;
  getDept(id)
    .then(response => {
      Object.assign(formData, enum2Value(response.data));
      formData.parentId = response.data.parentId > 0 ? response.data.parentId : undefined;
    })
    .finally(() => {
      loading.value = false;
    });
};

const handleAdd = (parentId: number) => {
  Object.assign(
    formData,
    reactive({
      deptId: undefined,
      parentId: parentId > 0 ? parentId : undefined,
      sort: 99,
      status: AvailableStatusEnum.ENABLE.code
    })
  );

  title.value = "添加部门";
  visible.value = true;
};

const handleEdit = (id: number) => {
  handleGetDept(id);
  title.value = "编辑部门";
  visible.value = true;
};

const handleClose = () => {
  formRef.value.resetFields();
  visible.value = false;
};

const handleSubmit = () => {
  formRef.value
    .validate()
    .then(() => {
      loading.value = true;
      if (formData.deptId == undefined) {
        saveDept(formData)
          .then(() => {
            antdMessage.success("添加成功");
            emit("reload");
            handleClose();
          })
          .finally(() => {
            loading.value = false;
          });
      } else {
        updateDept(formData)
          .then(() => {
            antdMessage.success("编辑成功");
            emit("reload");
            handleClose();
          })
          .finally(() => {
            loading.value = false;
          });
      }
    })
    .catch(error => {
      //console.log("error", error);
    });
};

defineExpose({
  handleAdd,
  handleEdit
});
</script>
